<?php		



class RbacFace extends FaceBase{

	private $nohf = array('');
	private $adhf = array("index","privateindex","topindex");

	private $api = null;

	/**
	 * 构造函数，初始化插件
	 */
	public function __construct() {
		parent::__construct();
		
		$this->api = n_api("rbac");
		//将标准头加进来，在析构函数中加了标准尾。对于风格的变化，只需要调整标准头标准尾
		//n_api("user")->isLogin();	
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header();
		}

	}
	
	public function index(){
		$mysql = n_api("mysql")->db001;
		
		$rs['role'] = $mysql->select("select * from rbac_role");
		$rs['private'] = $this->api->getPrivateAll();
		$siteRole = $mysql->select("select * from rbac_role_main");
		if(!empty($siteRole)){
			foreach($siteRole as $key=>$val){
				$rs['siteRole'][$val['role_id']][$val['scode']] = $val['private_ids'];
			}
		}
		//print_r($rs);exit;
		$this->_html("index",$rs);
	}
	public function privateIndex(){
		$mysql = n_api("mysql")->db001;
		
		$rs['private'] = $mysql->select("select * from rbac_private order by pevent");
		//print_r($rs);exit;
		$this->_html("privateIndex",$rs);
		
	}
	
	public function privateEdit(){
		$param = $this->param("id:uint:ignore");
		$rs = array();
		if(!empty($param['id'])){
			$rs['info'] = n_api("mysql")->db001->get("rbac_private",$param['id']);
			if(empty($rs['info'])){
				throw new PException("找不到权限");
			}
		}
		$this->_html("privateEdit",$rs);
	}
	public function privateSave(){
		$param = $this->param("name:char,acode:char,pevent:char,issite:char,id:uint:ignore");
		$mysql = n_api("mysql")->db001;
		$data['name'] = $param['name'];
		$data['acode'] = $param['acode'];
		$data['pevent'] = $param['pevent'];
		$data['issite'] = $param['issite'];
		
		if(empty($param['id'])){
			$mysql->save("rbac_private",$data);
		}else{
			$mysql->update("rbac_private",$data,"id={$param['id']}");
		}
		n_mod("skin")->save("保存成功");
		//$this->_html("roleSave",$rs);
	}
	
	public function topIndex(){
		$param = $this->param("page:page");
		
		$mysql = n_api("mysql")->db001;
		$perpage = 20;
		$start = ($param['page']-1)*$perpage;
		$rs['private'] = $this->api->getTop();
		$rs['data'] = $mysql->select("select * from rbac_user_main order by utime desc limit {$start},{$perpage}");
		$rs['paging'] = n_api("common")->paging(n_url("rbac","topIndex"),$param['page'],$mysql->count(),$perpage);
		$this->_html("topIndex",$rs);
	}
	public function topEdit(){
		$param = $this->param("passport:char:ignore");
		$rs = $param;
		
		$mysql = n_api("mysql")->db001;
		if(!empty($param['passport'])){
			$pinfo = n_api("passport")->getByEmail($param['passport']);
			if(empty($pinfo)){
				$rs['msg'] = "找不到该用户";
			}else{
				$rs['pinfo'] = $pinfo;
				print_r($pinfo);
				$rs['pps'] = $mysql->fetchFirst("select * from rbac_user_main where ppid={$pinfo['id']}");
				
			}
		}
		$rs['private'] = $this->api->getTop();
		$this->_html("topEdit",$rs);
	}
	public function topSave(){
		$param = $this->param("ppid:uint");
		
		if(!empty($_POST['pid'])){
		foreach($_POST['pid'] as $key=>$val){
			$pid[] = intval($val);
		}
		}
		$data['ppid'] = $param['ppid'];
		$data['private_ids'] = implode(',',$pid);
		$data['utime'] = N_NOW;
		n_api("mysql")->db001->save("rbac_user_main",$data);
		n_mod("skin")->save("保存成功");
	}
	public function topDel(){
		$param = $this->param("ppid:uint");
		
		n_api("mysql")->db001->exec(" delete from rbac_user_main where ppid={$param['ppid']}");
		n_mod("skin")->save("删除成功");
	}

	public function test(){
		$param = $this->param("ppid:uint");
		print_r($this->api->getUserPrivate($param['ppid']));
	}
	
	public function roleEdit(){
		$param = $this->param("id:uint:ignore");
		$rs = array();
		if(!empty($param['id'])){
			$rs['info'] = n_api("mysql")->db001->get("rbac_role",$param['id']);
			if(empty($rs['info'])){
				throw new PException("找不到角色");
			}
		}
		$this->_html("roleEdit",$rs);
	}
	public function roleSave(){
		$param = $this->param("name:char,id:uint:ignore");
		$mysql = n_api("mysql")->db001;
		if(empty($param['id'])){
			$mysql->save("rbac_role",$param['name']);
		}else{
			$mysql->update("rbac_role",array('name'=>$param['name']),"id={$param['id']}");
		}
		n_mod("skin")->save("保存成功");
		//$this->_html("roleSave",$rs);
	}
	
	
	public function sitePrivateList(){
		$param = $this->param("scode:char");
		$mysql = n_api("mysql")->db001;
		$rs['scode'] = $param['scode'];
		$rs['role'] = $mysql->select("select * from rbac_role_main as a
												left join rbac_role as b on a.role_id=b.id
												where scode='{$param['scode']}'");
		$rs['private'] = $mysql->selectIndex("select * from rbac_private where issite='Y'");
		$this->_html("sitePrivateList",$rs);
	}
	public function sitePrivateToggle(){
		$param = $this->param("private:uint,role:uint,scode:char");
		$mysql = n_api("mysql")->db001;
				
		$roleMain = $mysql->fetchFirst("select * from rbac_role_main where role_id={$param['role']} and scode='{$param['scode']}'");
		if(empty($roleMain)){
			throw new PException("找不到权限");
		}
		$privates = explode(',', $roleMain['private_ids']);
		$kid = array_search($param['private'],$privates);
		if(empty($kid)){
			$privates[] = $param['private'];
		}else{
			unset($privates[$kid]);
		}
		$data['private_ids'] = implode(',', $privates);
			
			
		$rs['data'] = $mysql->update("rbac_role_main",$data,"role_id={$param['role']} and scode='{$param['scode']}'");
		$this->sitePrivateList();
		
	}
	
	public function siteRoleEdit(){
		$param = $this->param("scode:char");
		$mysql = n_api("mysql")->db001;
		$rs['scode'] = $param['scode'];
		$rs['role'] = $mysql->select("select a.*,b.id as rpid,b.isdel from rbac_role as a
												left join rbac_role_main as b on a.id=b.role_id and scode='{$param['scode']}'");
		print_r($rs);
		
		$this->_html("siteRoleEdit",$rs);
	}
	
	
	public function siteRoleSave(){
		$param = $this->param("roleid:uint,scode:char");
		$mysql = n_api("mysql")->db001;
		
		$data['role_id'] = $param['roleid'];
		$data['scode'] = $param['scode'];
		$data['ctime'] = time();
		$rinfo = $mysql->fetchFirst("select * from rbac_role_main where role_id='{$data['role_id']}' and scode='{$data['scode']}'");
		if(empty($rinfo)){
			$rs['data'] = $mysql->insert("rbac_role_main",$data);
		}elseif('Y'==$rinfo['isdel']){
			$mysql->undel("rbac_role_main",$rinfo['id']);
		}elseif('N'==$rinfo['isdel']){
			$mysql->del("rbac_role_main",$rinfo['id']);
		}
		$this->siteRoleEdit();
		//$this->_html("siteRoleSave",$rs);
		
	}
	
	
	/**
	 * 站点角色授权
	 */
	public function siteRoleDeputeList(){
		$param = $this->param("rmid:uint");
		$rs['rmid'] = $param['rmid'];
		$rs['member'] = n_api("member")->getAllUname();
		$rs['depute'] = array_keys(n_api("mysql")->db001->selectIndex("select ppid from rbac_main where rmid={$param['rmid']}",'ppid'));
		
		$this->_html('siteRoleDeputeList',$rs);
	}

	/**
	 * 站点角色授权[简化版本]
	 */
	public function siteRoleDeputeEdit(){
		$param = $this->param("rmid:uint");
		$mysql = n_api("mysql")->db001;
		$passport = n_api("passport");
		$rs['rmid'] = $param['rmid'];
		$member = $mysql->selectIndex("select ppid from rbac_main where rmid={$param['rmid']}",'ppid');
		$rs['mlist'] = '';
		if(!empty($member)){
			$plist = $passport->get(array_keys($member));
			foreach($plist as $key=>$val){
				$mlist[] = $val['uname'];
			}
			$rs['mlist'] = implode(',',$mlist);
		}
		$this->_html('siteRoleDeputeEdit',$rs);
	}

	public function siteRoleDeputeSave(){
		$param = $this->param("rmid:uint,member:char");
		$mysql = n_api("mysql")->db001;
		$pp = n_api("passport");
		$member = $mysql->selectIndex("select id,ppid from rbac_main where rmid={$param['rmid']}",'ppid');
		if(!empty($param['member'])){
			$mlist = explode(",",$param['member']);
			$mlist = array_unique($mlist);
			foreach($mlist as $key=>$val){
				$ppInfo = $pp->getByUname($val);
				if(empty($ppInfo)){
					$rs['err'][] = $val;
					continue;
				}
				if(isset($member[$ppInfo['id']])){
					$rs['exists'][] = $ppInfo['uname'];
					unset($member[$ppInfo['id']]);
					continue;
				}
				$new[] = $ppInfo['id'];
				$rs['new'][] = $val;
			}
		}
		//删除的用户
		if(!empty($member)){
			foreach($member as $key=>$val){
				$ppInfo = $pp->get($val['ppid']);
				$rs['rm'][] = $ppInfo['uname'];
				$mysql->rm("rbac_main",$val['id']);
			}
		}
		//新增加的用户
		if(!empty($new)){
			$data['rmid'] = $param['rmid'];
			$data['ctime'] = N_NOW;
			foreach($new as $key=>$val){
				$data['ppid'] = $val;
				$mysql->insert("rbac_main",$data);
			}
		}
		$rsStr = '';
		if(!empty($rs['err']))$rsStr .= '错误账号：'.implode(',',$rs['err'])."<br />";
		if(!empty($rs['new']))$rsStr .= '新增账号：'.implode(',',$rs['new'])."<br />";
		if(!empty($rs['rm']))$rsStr .= '删除账号：'.implode(',',$rs['rm'])."<br />";
		if(!empty($rsStr))$rsStr = "<br /><br /><div class='n-notice'>".$rsStr."</div>";
		n_mod("skin")->save("保存成功".$rsStr);
	}
	
	public function siteRoleDeputeToggle(){
		$param = $this->param("ppid:uint,rmid:uint");
		$mysql = n_api("mysql")->db001;
		
		$sql = "select * from rbac_main where ppid={$param['ppid']} and rmid={$param['rmid']}";
		$main = $mysql->fetchFirst($sql);
		
		$rmInfo = $mysql->fetchFirst("select a.scode,b.name from rbac_role_main as a
														left join rbac_role as b on a.role_id=b.id
														where a.id={$param['rmid']}
														");
		if(empty($rmInfo)){
			throw new PException("找不到角色关系");
		}
		
		$sinfo = n_api("nsite")->getByScode($rmInfo['scode']);
		if(empty($sinfo)){
			throw new PException("找不到网站");
		}
		
		$uname = n_api("member")->getAval($param['ppid'],'uname');
		if(empty($main)){
			$param['ctime'] = time();
			$mysql->insert("rbac_main",$param);
			$msg = "@{$uname}@加入了项目组【{$rmInfo['name']}】角色";
			$fmsg = "@{$uname}@加入了项目组【{$sinfo['name']}】的【{$rmInfo['name']}】角色";
		}else{
			$mysql->rm('rbac_main',$main['id']);
			$msg = "@{$uname}@退出了项目组【{$rmInfo['name']}】角色";
			$fmsg = "@{$uname}@退出了项目组【{$sinfo['name']}】的【{$rmInfo['name']}】角色";
		}

		$this->siteRoleDeputeList($rmid);
		n_api("eafeed")->pushArr($rmInfo['scode'],$_SESSION['ppid'],array("content"=>$msg),array('toPpid'=>n_api("rbac")->getSitePpid($rmInfo['scode']),'toMsg'=>$msg));
	}
	
	
	
	
	public function __destruct(){
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->footer();
		}
	}
	
}


